package dao.impl;

import dao.BooksDao;
import pojo.Books;


import java.sql.SQLException;
import java.util.ArrayList;

public class BooksDaoImpl extends BaseDao implements BooksDao{


    //获取图书信息
    @Override
    public ArrayList<Books> getBookList(){
        ArrayList<Books> booksList = new ArrayList<Books>();
        Books books = null;

        //建立数据库连接
        this.conn = this.getConnection();

        try {
            this.state = this.conn.createStatement();
            this.rs = this.state.executeQuery("Select * from book");

            while (rs.next()) {
                books = new Books();
                books.setbID(rs.getInt("bID"));
                books.settID(rs.getInt("tID"));
                books.setbName(rs.getString("bName"));
                books.setbAutho(rs.getString("bAutho"));
                books.setbCode(rs.getString("bCoad"));
                books.setbNum(rs.getInt("bNum"));

                booksList.add(books);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println(booksList.size());
        return booksList;
    }


    //删除图书
    @Override
    public int delBookbyID(int bID) {
        int res = 0;
        // 建立连接
        this.conn = this.getConnection();
        try {
            this.pst =this.conn.prepareStatement("DELETE FROM book where bID = ? ");
            pst.setInt(1,bID);

            res = pst.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }

    // 通过ID查找书籍
    @Override
    public Books searchBookByID(int bID) {

        Books books = null;

        // 建立连接
        this.conn = this.getConnection();
        try {
            this.pst = this.conn.prepareStatement("Select * from goods WHERE gdID = ?");
            pst.setInt(1,bID);

            this.rs = pst.executeQuery();
            while (rs.next()){
                books = new Books();
                books.setbID(rs.getInt("bID"));
                books.settID(rs.getInt("tID"));
                books.setbName(rs.getString("bName"));
                books.setbAutho(rs.getString("bAutho"));
                books.setbCode(rs.getNString("bCode"));
                books.setbNum(rs.getInt("bNum"));

            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return books;
    }


    //更新书籍
    @Override
    public int updateBook(Books books) {
        String strSQl= "UPDATE book " +
                "SET  bName = ? " +
                ",bAutho = ?" +
                ",tID = ?" +
                ",bCode = ?" +
                ",bNum = ?  WHERE  gdID = ?";
        int res = 0;
        // 建立连接
        this.conn = this.getConnection();
        try {
            this.pst =this.conn.prepareStatement(strSQl);
            pst.setString(1,books.getbName());
            pst.setString(2,books.getbAutho());
            pst.setInt(3,books.gettID());
            pst.setString(4,books.getbCode());
            pst.setInt(5,books.getbNum());
            pst.setInt(6,books.getbID());
            res = pst.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }

    //添加书籍
    @Override
    public int addBook(Books books) {
        String strSQl= "INSERT  INTO book(tID,bName,bAutho,bCode,bNum)" +
                "VALUES (?,?,?,?,?)";
        int res = 0;
        // 建立连接
        this.conn = this.getConnection();
        try {
            this.pst =this.conn.prepareStatement(strSQl);
            pst.setInt(1,books.gettID());
            pst.setString(2,books.getbName());
            pst.setString(3,books.getbAutho());
            pst.setString(4,books.getbCode());
            pst.setInt(5,books.getbNum());

            res = pst.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }
}
